package beans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import domain.UserDAO;
import exception.IBToolsException;

import manager.GrowlManager;
import manager.MapManager;
import manager.SessionManager;
import manager.UserManager;

@ManagedBean(name="sessionBean")
@ViewScoped
public class SessionBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9154609252999729708L;

	public boolean isAdminUser(){
		return SessionManager.isAdminUser();
//		return false;
	}
	public String getLoggedIn() {
		return SessionManager.isLoggedIn();
	}
	public void setLoggedIn(String loggedIn) {
		SessionManager.setLoggedIn("true");
	}
	public boolean isLogging(){
		return SessionManager.isLogging();
	}
	public void setLogging(boolean logging){
		SessionManager.setLogging(logging);
	}
	public void reloadExcReasons() throws IBToolsException{
		MapManager.initErrorReasonMap();
	}
	public void reloadBagKeys(){
//		MapManager.initBagKeyMap(false);
	}
	public void setGlobalUserCode(String globalUserCode){
		SessionManager.setGlobalUserCode(globalUserCode.toUpperCase());
	}
	public String getSessionUserCode(){
		return SessionManager.getLoginUserCode();
	}
	public String getGlobalUserCode(){
		return null;
	}
	public void switchTheme(String theme){
		try {
			UserDAO user = UserManager.getUserInfoFromDB(SessionManager.getLoginUserCode());
			if(user == null){
				user = UserManager.newUserInstance(SessionManager.getLoginUserCode());
			}
			user.setCurrentTheme(theme);
			UserManager.saveUserToDB(user);
		} catch (IBToolsException e) {
			e.printStackTrace();
		}
		SessionManager.setTheme(theme);
	}
	public String getTheme(){
		String theme = SessionManager.getTheme();
		if(theme == null || theme.equalsIgnoreCase(""))
			theme = "start";
		return theme;
	}
	public String getClipBoard(){
		String clipBoard = SessionManager.getClipBoard();
		return clipBoard;
	}
	public void setClipBoard(String clipBoard){
		SessionManager.setClipBoard(clipBoard);
	}
	public void refreshSession(){
		SessionManager.reloadSession();
		GrowlManager.addInfo("Session Reloaded", 
				"<b>User:</b> " + SessionManager.getLoginUserCode() + "<br/>" + 
						"<b>Width:</b> " + SessionManager.getWidth() + "<br/>" + 
						"<b>Height:</b> " + SessionManager.getHeight() + "<br/>");
	}
}
